package com.atomic.test.example.service.impl;

import com.atomic.test.example.entity.Board;
import com.atomic.test.example.entity.Topic;
import com.atomic.test.example.exception.CRUDException;
import com.atomic.test.example.exception.ExceptionCode;
import com.atomic.test.example.repository.BoardRepository;
import com.atomic.test.example.repository.TopicRepository;
import com.atomic.test.example.service.ITopicService;
import com.atomic.test.example.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * Created by dreamyao on 2017/3/6.
 */
@Service
public class TopicServiceImpl implements ITopicService {

    @Autowired
    private BoardRepository boardRepository;
    @Autowired
    private TopicRepository topicRepository;

    /**
     * 创建话题
     *
     * @param topic
     * @return
     */
    @Override
    public Integer createTopic(Topic topic) {
        Topic queryTopic = topicRepository.findTopicByTopicTitle(topic.getTopicTitle());
        if (queryTopic != null) {
            throw new CRUDException(ExceptionCode.HAVE_REPEAT_DATA, "存在重复数据！");
        }
        final Optional<Board> optional = boardRepository.findById(topic.getBoardId());
        if (!optional.isPresent()) {
            throw new CRUDException(ExceptionCode.HAVE_NOT_DATA, "论坛板块不存在！");
        }
        topic.setUserId(UserContext.getLoginUser().getId());
        topic.setCreateTime(new Date());
        topic.setLastPost(null);
        topic.setDigest(0);
        topic.setTopicReplies(0);
        topic.setTopicViews(0);
        final Topic save = topicRepository.save(topic);

        Integer boardNum = boardRepository.updateTopicNumByBoardId(topic.getBoardId(),
                optional.get().getTopicNum() + 1);
        if (boardNum == 0) {
            throw new CRUDException(ExceptionCode.DATA_INSERT_ERROR, "话题数更新失败！");
        }
        return topic.getId();
    }

    /**
     * 更新话题
     *
     * @param topic
     */
    @Override
    public void updateTopic(Topic topic) {
        Topic queryTopic = topicRepository.findTopicById(topic.getId());
        queryTopic.setBoardId(topic.getBoardId());
        queryTopic.setTopicTitle(topic.getTopicTitle());
        queryTopic.setUserId(UserContext.getLoginUser().getId());
        queryTopic.setDigest(topic.getDigest());
        topicRepository.save(queryTopic);
    }

    /**
     * 根据话题Id查询话题信息
     *
     * @param topicId
     * @return
     */
    @Override
    public Topic findOneTopicById(Integer topicId) {
        Topic queryTopic = topicRepository.findTopicById(topicId);
        if (queryTopic == null) {
            throw new CRUDException(ExceptionCode.HAVE_NOT_DATA, "无对应的数据!");
        }
        return queryTopic;
    }

    /**
     * 查询所有话题
     *
     * @return
     */
    @Override
    public List<Topic> findAllTopic() {
        return topicRepository.findAll();
    }

    /**
     * 根据话题标题查询话题信息
     *
     * @param topicTitle 话题标题
     * @return 话题信息
     */
    @Override
    public Topic findTopicByTopicTitle(String topicTitle) {
        return topicRepository.findTopicByTopicTitle(topicTitle);
    }

    /**
     * 根据话题标题删除话题
     *
     * @param topicTitle
     */
    @Override
    public void deleteTopicByTopicTitle(String topicTitle) {
        Topic queryTopic = topicRepository.findTopicByTopicTitle(topicTitle);
        if (queryTopic == null) {
            throw new CRUDException(ExceptionCode.HAVE_NOT_DATA, "需要删除的话题不存在！");
        }
        Board board = boardRepository.findBoardById(queryTopic.getBoardId());
        topicRepository.deleteTopicByTopicTitle(topicTitle);
        boardRepository.updateTopicNumByBoardId(queryTopic.getBoardId(), board.getTopicNum() - 1);
    }
}
